package cn.hbwe.controller;


import cn.hbwe.Dto.pLoginDTO;
import cn.hbwe.Dto.pRegisterDTO;
import cn.hbwe.pojo.dto.DoctorDto;
import cn.hbwe.pojo.entity.Doctor;
import cn.hbwe.pojo.entity.Patient;
import cn.hbwe.service.IDoctorService;
import cn.hbwe.service.IPatientService;
import cn.hbwe.util.Result;
import cn.hbwe.util.constant.BaseContext;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */

@RequestMapping("/patient")
@RestController
@Api(tags = "病人登录相关方法")
@CrossOrigin
public class PatientController {


    @Autowired
    private IPatientService patientService;

    @Autowired
    private IDoctorService doctorService;


    /**
     * 用户登录
     *
     * @param loginDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/plogin")
    @ApiOperation("用户登录")
    public Result<Patient> login(@RequestBody @Valid pLoginDTO loginDTO, BindingResult bindingResult, HttpServletRequest request) {
        List<ObjectError> errors = bindingResult.getAllErrors();
        for (ObjectError error : errors) {
            return Result.error(error.getDefaultMessage());
        }
        Patient patient = patientService.plogin(loginDTO);
        Integer id = patient.getPatientId();
        BaseContext.setCurrentId(id);
        return Result.success(patient);
    }

    //--------------------------------------------------------------------------------------------------------------


    /**
     * 用户注册
     *
     * @param pregisterDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/pregister")
    @ApiOperation("用户注册")
    public Result<Patient> register(@RequestBody @Valid pRegisterDTO pregisterDTO, BindingResult bindingResult) {
        List<ObjectError> errors = bindingResult.getAllErrors();
        for (ObjectError error : errors) {
            return Result.error(error.getDefaultMessage());
        }
        Patient patient = patientService.pregister(pregisterDTO);
        return Result.success(patient);
    }


    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public Result<Patient> modifyPatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.success(patient);

    }





    /**
     * 根据科室id查询医生信息
     * @param Id
     */
    @ApiOperation("根据用户账号查询用户信息")
    @GetMapping("/getPatientId/{Id}")
    public Result<List<Patient>> getByParientAccount(@PathVariable int Id) {
        List<Patient> patient = patientService.getPatientId(Id);
        System.out.println(patient);
        return Result.success(patient);
    }


    @ApiOperation("删除患者")
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        patientService.removeById(id);
        return Result.success("删除患者成功");
    }



    @ApiOperation("患者分页查询")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result<IPage<Patient>> getPage(@PathVariable Long currentPage, @PathVariable Long pageSize, String name) {
        IPage<Patient> page = patientService.getPage(currentPage,pageSize,name);
        return Result.success(page);
    }



    //----------------查看用户名字-------------------------
    @GetMapping("/getPname/{pid}")
    public Result getPname(@PathVariable Integer pid) {
        String pname=patientService.pname(pid);
        System.out.println(pname);
        return Result.success(pname);
    }








}
